package com.chenzhou.controller;

import com.chenzhou.common.Encrypt;
import com.chenzhou.entity.UserEntity;
import com.chenzhou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserService service;

    @RequestMapping("/login")
    public String user_Login(HttpSession session,String uname,String password){
        ///TODO 使用unane和password验证用户的合法性，去数据库查询。
        String pwd = Encrypt.md5AndSha(password);//加密验证
        UserEntity userEntity = service.queryUserByUnameAndPassward(uname,pwd);
        boolean flag = null != userEntity?true:false;
        if(flag){
            ///TODO 存储当前登录用户信息
            if(userEntity.getIsadmin().equals("Y")) {
                session.setAttribute("user", userEntity);
                return "redirect:/user/login_main";
            }else{
                session.setAttribute("msg","权限不够，请联系管理员！！！");
                return "redirect:/login.jsp";
            }
        }else{
            ///TODO 当前用户不合法
            session.setAttribute("msg","用户名或者密码错误！！！");
            return "redirect:/login.jsp";
        }
    }

    @RequestMapping("/login_out")
    public String login_out(HttpSession session){
        ///TODO 销毁session中存储的用户信息
        session.removeAttribute("user");
        session.removeAttribute("msg");
        return "redirect:/login.jsp";
    }

    @RequestMapping("/login_main")
    public String login_main(HttpServletRequest request){
        ///TODO 跳转主页面，并且查询信息保存到request里面

        request.setAttribute("menu","Dashboard");
        return "main";
    }
}
